package response

import (
    "ginblog/model"
    "ginblog/utils"
    "github.com/gin-gonic/gin"
    "net/http"
)

type Res struct {
    Code model.Code  `json:"code"`
    Data interface{} `json:"data,omitempty"`
    Msg  string      `json:"msg,omitempty"`
}

func Result(code model.Code, data interface{}, c *gin.Context, msg string) {
    c.JSON(http.StatusOK, Res{
        code,
        data,
        utils.IF(msg == "", code.GetMsg(), msg).(string),
    })
}

func Ok(c *gin.Context, data interface{}, msg ...string) {
    var m string
    if len(msg) > 0  {
        m = msg[0]
    }
    Result(model.SUCCESS, data, c, m)
}

func Fail(c *gin.Context, errCode model.Code, data interface{}, msg ...string) {
    var m string
    if len(msg) > 0  {
        m = msg[0]
    }
    Result(errCode, data, c, m)
}
